package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.qo.EmployeeQueryObject;
import cn.wolfcode.crm.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class EmployeeService implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public void saveOrUpdate(Employee employee, Long[] ids) {
        if(employee.getId() != null){
            employeeMapper.updateByPrimaryKey(employee);
            employeeMapper.deleteRelationEmployeeAndRole(employee.getId());
        } else {
            //添加员工的时候需要加密
            Md5Hash md5Hash = new Md5Hash(employee.getPassword(), employee.getName(), 3);
            employee.setPassword(md5Hash.toString());
            employeeMapper.insert(employee);
        }
        if(ids != null ){
            for (Long roleId : ids) {
                employeeMapper.insertRelationEmployeeAndRole(employee.getId(),roleId);
            }
        }
    }

    @Override
    public void delete(Long id) {
        if(id != null){
            employeeMapper.deleteByPrimaryKey(id);
            employeeMapper.deleteRelationEmployeeAndRole(id);
        }
    }

    @Override
    public Employee getEmployeeById(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> getAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public PageInfo<Employee> selectByQuery(EmployeeQueryObject queryObject) {
//        int totalCount = employeeMapper.countByQuery(queryObject);
//        if(totalCount == 0){
//            return new PageResult(1,queryObject.getPageSize(),0, Collections.emptyList());
//        }
        PageHelper.startPage(queryObject.getCurrentPage(),queryObject.getPageSize());
        List<Employee> employees = employeeMapper.selectByQuery(queryObject);
        return new PageInfo<>(employees);
    }

    @Override
    public Employee getEmployeeByNameAndPassword(String name, String password) {

        return employeeMapper.getEmployeeByNameAndPassword(name,password);
    }

    @Override
    public void batchDelete(Long[] ids) {
        if(ids !=null){
            employeeMapper.batchDelete(ids);
            employeeMapper.batchDeleteRelation(ids);
        }
    }

    @Override
    public void importXls(MultipartFile file) throws IOException {
        //拿传入的数据 转成wb
        HSSFWorkbook wb = new HSSFWorkbook(file.getInputStream());
        //拿到表格中的sheet
        HSSFSheet sheet = wb.getSheet("员工信息");
        //HSSFSheet sheetAt = wb.getSheetAt(0);
        //遍历拿到每一行的数据,

        for (int i = 0; i< sheet.getLastRowNum();i++) {
            HSSFRow row = sheet.getRow(i + 1);
            String name = row.getCell(0).toString();//可以用getStringValue
            System.out.println(row.getCell(0).toString());
            String email = row.getCell(1).toString();
            int age = (int) row.getCell(2).getNumericCellValue();
            Employee employee = new Employee();
            employee.setName(name);
            employee.setEmail(email);
            employee.setAge(age);
            employee.setPassword(new Md5Hash("1", name, 3).toString());
            employeeMapper.insert(employee);
        }
    }

    @Override
    public List<Employee> queryByRoleSn(String[] roleSns) {
        return employeeMapper.queryByRoleSn(roleSns);
    }


}
